var Position = function ( positionId, positionName ){
	// attributes
	this.positionId   = positionId;
	this.positionName = positionName;
	
	// variables
	var a = this
		,i = 0
		,d = undefined
		,c = undefined
		,m = undefined
		,mc = undefined
		;
	
	// callback function for the map's onClick handler.
	mc = function ( lon, lat, zoom ){
		//console.log('mapSelect: lon '+lon+' lat '+lat+' zoom '+zoom);
		jQuery('#'+a.getDivId('lon', true)).val(lon);
		jQuery('#'+a.getDivId('lat', true)).val(lat);
		jQuery('#'+a.getDivId('zoom', true)).val(zoom);
	};
	
	// show overlay
	this.overlay = new DPOverlay();
	this.overlay.setContent('<div class="dpposition_a_o_top clearfix">'
			+'<div class="dpposition_a_o_title">'+a.positionName+'</div>'
			+'<div id="'+this.getDivId('close', true)+'" class="dpposition_a_o_close"></div>'
		+'</div>'
		+'<div class="dpposition_a_o_main">'
			+'<form id="'+this.getDivId('form', true)+'" method="post" action="index.php">'
				+'<input type="hidden" name="Itemid" value="'+dppositionParams.Itemid+'" />'
				+'<input type="hidden" name="option" value="com_dpposition" />'
				+'<input type="hidden" name="task" value="saveposition" />'
				+'<input id="'+this.getDivId('id', true)+'" type="hidden" name="dpposition_id[]" value="'+this.positionId+'" />'
				+'<input id="'+this.getDivId('lon', true)+'" type="hidden" name="dpposition_lon[]" value="" />'
				+'<input id="'+this.getDivId('lat', true)+'" type="hidden" name="dpposition_lat[]" value="" />'
				+'<input id="'+this.getDivId('zoom', true)+'" type="hidden" name="dpposition_zoom[]" value="" />'
				+'<div id="'+this.getDivId('position', true)+'" class="dpposition_a_o_position">'
					+'<div class="dpposition_a_o_loading">Ladataan paikkatietoja</div>'
				+'</div>'
			+'</form>'
		+'</div>'
		+'<div class="dpposition_a_o_bottom clearfix">'
			+'<div id="'+this.getDivId('cancel', true)+'" class="dpposition_a_o_cancel">Peruuta</div>'
			+'<div id="'+this.getDivId('send', true)+'" class="dpposition_a_o_send">Tallenna</div>'
		+'</div>');
	
	// get position data
	jQuery.ajax({
		url: "index2.php",
		type: "POST",
		dataType: "json",
		data: {
			option:  'com_dpposition',
			task:    'getposition',
			id:      this.positionId
		},
		success: function ( json ){
			if( json.status == 'ok' ){
				// set hidden fields
				a.positionId   = json.dpposition_id;
				a.positionName = json.dpposition_name;
				jQuery('#'+a.getDivId('id', true)).val(json.dpposition_id);
				jQuery('#'+a.getDivId('lon', true)).val(json.dpposition_lon);
				jQuery('#'+a.getDivId('lat', true)).val(json.dpposition_lat);
				jQuery('#'+a.getDivId('zoom', true)).val(json.dpposition_zoom);
				
				// show form elements
				jQuery('#'+a.getDivId('position', true)).empty();
				jQuery('#'+a.getDivId('position', true)).html(
					a.getFormRowHTML(
						a.getLabelHTML('Paikan nimi', true)
						+a.getValueHTML('<input class="dpposition_a_o_text" type="text" name="dpposition_name[]" value="'+json.dpposition_name+'" />')
					)
					+a.getFormRowHTML(
						a.getLabelHTML('Katuosoite')
						+a.getValueHTML('<input class="dpposition_a_o_text" type="text" name="dpposition_street[]" value="'+json.dpposition_street+'" />')
					)
					//+a.getFormRowHTML(
					//	a.getLabelHTML('Kuvaus')
					//	+a.getValueHTML('<input class="dpposition_a_o_text" type="text" name="dpposition_details[]" value="'+json.dpposition_details+'" />')
					//)
					+a.getFormRowHTML(
						a.getLabelHTML('Julkaistu')
						+a.getValueHTML('<input type="checkbox"'+(json.dpposition_status == 1 ? ' checked="checked"' : '')+' name="dpposition_status[]" value="1" />')
					)
					+a.getFormRowHTML(
						//a.getLabelHTML('Sijainti kartalla')
						a.getValueHTML('<div id="'+a.getDivId('map', true)+'" class="dpposition_a_o_map"></div>')
					)
				);
				
				// show map
				m = new OLDPPositionMap({
					formID:     a.getDivId('form', true),
					divID:      a.getDivId('map', true),
					iconUrl:    dppositionParams.iconUrl,
					iconWidth:  dppositionParams.iconWidth,
					iconHeight: dppositionParams.iconHeight,
					callback:   mc,
					mode:       OLDPPositionMap.MODE_SELECT
				});
				m.addPosition(json.dpposition_lon,json.dpposition_lat,json.dpposition_zoom);
				// closest zoom
				m.centerMap();
			}else if( json.status == 'error' ){
				alert(json.error);
			}
		}
	});
	
	// bind ok
	jQuery('#'+this.getDivId('send', true)).bind('click', function ( e ){
		a.submitData();
		e.preventDefault();
	});
	
	// bind close and cancel
	var c = function ( e ){
		a.closeOverlay();
		e.preventDefault();
	};
	jQuery('#'+this.getDivId('close', true)).bind('click', c);
	jQuery('#'+this.getDivId('cancel', true)).bind('click', c);
};

// methods
Position.prototype = {
	getDivId: function ( name, overlay ){
		return 'dpposition_a'+(overlay ? '_o' : '')+'_position_'+this.positionId+(name ? '_'+name : '');
	},
	
	getFormRowHTML: function( row ){
		return '<div class="dpposition_a_o_f_row clearfix">'+row+'</div>';
	},
	
	getRequiredHTML: function( required ){
		return '<div class="dpposition_a_o_f_required">'+(required ? '*' : '&nbsp;')+'</div>';
	},
	
	getLabelHTML: function( label, required ){
		if( !label ){
			label = '&nbsp';
		}else if( label.length < 1 ){
			label = '&nbsp';
		}
		return '<div class="dpposition_a_o_f_label">'+this.getRequiredHTML(required)+label+'</div>';
	},
	
	getValueHTML: function( value ){
		return '<div class="dpposition_a_o_f_value">'+value+'</div>';
	},
	
	closeOverlay: function (  ) {
		this.overlay.remove();
	},
	
	submitData: function ( ) {
		// post the data
		jQuery('#'+this.getDivId('form', true)).get(0).submit();
		this.closeOverlay();
	}
};